Page({
  data: {
<<<<<<< HEAD
<<<<<<< HEAD
    currentDate: '7月21日',
=======
    currentDate: '',
>>>>>>> d89ffbc... 07-23统计图
=======
    currentDate: '',
>>>>>>> 0856d6b... 07-25完成小程序
    latestRecord: {}, // 初始化为空对象
    details: [],
    categoryIcons:{
      '请选择':'/staic/请选择.png',
      '餐饮': '/staic/00 (2).png' ,
      '交通': '/staic/00 (5).png' ,
      '服饰': '/staic/001 (22).png',
      '购物': '/staic/00 (3).png' ,
      '服务': '/staic/001 (6).png' ,
      '教育': '/staic/001 (7).png' ,
      '娱乐': '/staic/001 (21).png' ,
      '运动': '/staic/001 (23).png' ,
      '生活缴费': '/staic/00 (6).png' ,
      '旅行': '/staic/001 (10).png' ,
      '宠物': '/staic/001 (3).png' ,
      '医疗': '/staic/001 (20).png' ,
      '保险': '/staic/001 (1).png' ,
      '公益': '/staic/001 (24).png' ,
      '发红包': '/staic/001 (5).png' ,
      '转账': '/staic/001 (19).png' ,
      '亲属卡': '/staic/001 (13).png' ,
      '其他': '/staic/001 (12).png' ,
      '退还': '/staic/001 (18).png' 
    },
    multiDateOptions: [
      Array.from({ length: 2100 }, (_, i) => i + 0), // 年份
      Array.from({ length: 12 }, (_, i) => i + 0), // 月份
    ],
    selectedDate: [2024, 7], // 初始化为当前日期的索引
    totalIncome: 0,
    totalExpense: 0,
<<<<<<< HEAD
<<<<<<< HEAD
=======
    todayTotalExpense:0.00,
    todayTotalIncome:0.00,
>>>>>>> d89ffbc... 07-23统计图
=======
    todayTotalExpense:0.00,
    todayTotalIncome:0.00,
>>>>>>> 0856d6b... 07-25完成小程序
    records: [], // 存储从本地加载的记账记录
  },

  onLoad: function () {
    this.loadRecords();
<<<<<<< HEAD
<<<<<<< HEAD
  },
  loadRecords: function() {
    let records = wx.getStorageSync('records') || [];
    this.setData({ details: records });
  },
=======
=======
>>>>>>> 0856d6b... 07-25完成小程序
    this.updateDate();
    this.calculateDailyTotals();
  },
  
<<<<<<< HEAD
>>>>>>> d89ffbc... 07-23统计图
=======
>>>>>>> 0856d6b... 07-25完成小程序
  // 监听页面显示时重新加载数据
  onShow: function() {
    this.loadRecords();
    this.filterRecords();
<<<<<<< HEAD
<<<<<<< HEAD
=======
    this.calculateDailyTotals();
>>>>>>> d89ffbc... 07-23统计图
=======
    this.calculateDailyTotals();
>>>>>>> 0856d6b... 07-25完成小程序
  },
  // 过滤记录的函数
  filterRecords: function() {
    let activeCategory = wx.getStorageSync('activeCategory') || {};
    let records = this.data.details;
    let filteredRecords = records;

    if (activeCategory.label) {
      if (activeCategory.label === '全部') {
        // 如果激活的类别是“全部分类”，则显示所有记录
        filteredRecords = records;
      } else {
        // 根据激活的类别过滤记录
        filteredRecords = records.filter(record => record.category === activeCategory.label);
      }
    }

    // 更新页面数据以显示对应类别的数据
    this.setData({
      details: filteredRecords,
      emptyMessage: filteredRecords.length > 0 ? '' : '没有找到相关数据'
    });
  },
  updateDate: function () {
<<<<<<< HEAD
<<<<<<< HEAD
    let date = new Date();
    let month = date.getMonth() + 1; // 注意月份是从0开始的
    let day = date.getDate();
    let formattedDate = `${month}月${day}日 今天`;
=======
=======
>>>>>>> 0856d6b... 07-25完成小程序
    const date = new Date();
    const month = date.getMonth() + 1; // 注意月份是从0开始的
    const day = date.getDate();
    const formattedDate = `${month}月${day}日`;
<<<<<<< HEAD
>>>>>>> d89ffbc... 07-23统计图
=======
>>>>>>> 0856d6b... 07-25完成小程序
    this.setData({
      currentDate: formattedDate
    });
  },
<<<<<<< HEAD
<<<<<<< HEAD
=======
=======
>>>>>>> 0856d6b... 07-25完成小程序

  loadRecords: function() {
    const records = wx.getStorageSync('records') || [];
    this.setData({ records });
    this.setMultiDateOptions(records);
  },
calculateDailyTotals: function() {
  const todayRecords = this.getTodayRecords();
  let todayTotalExpense = todayRecords.reduce((sum, record) => {
    return sum + (record.type === '支出' ? parseFloat(record.amount) : 0);
  }, 0);

  let todayTotalIncome = todayRecords.reduce((sum, record) => {
    return sum + (record.type === 'income' ? parseFloat(record.amount) : 0);
  }, 0);

  this.setData({
    todayTotalExpense: todayTotalExpense.toFixed(2),
    todayTotalIncome: todayTotalIncome.toFixed(2)
  });
},

// 新函数：获取今日记录
getTodayRecords: function() {
  const today = new Date();
  const todayYear = today.getFullYear();
  const todayMonth = today.getMonth() + 1;
  const todayDay = today.getDate();

  return this.data.details.filter(record => {
    const recordDate = new Date(record.date);
    return (
      recordDate.getFullYear() === todayYear &&
      recordDate.getMonth() + 1 === todayMonth &&
      recordDate.getDate() === todayDay
    );
  });
},
saveToLocal: function(newRecord) {
  let records = wx.getStorageSync('records') || [];
  records.push(newRecord);
  wx.setStorageSync('records', records);
  // 确保更新页面数据
  this.setData({ details: records });
  // 重新计算今日总计
  this.calculateDailyTotals();
},

<<<<<<< HEAD
>>>>>>> d89ffbc... 07-23统计图
=======
>>>>>>> 0856d6b... 07-25完成小程序
  loadRecords: function() {
    const records = wx.getStorageSync('records') || [];
    const totalIncome = wx.getStorageSync('totalIncome') || 0;
    const totalExpense = wx.getStorageSync('totalExpense') || 0;
    this.setData({
      details: records,
      totalIncome: totalIncome,
      totalExpense: totalExpense,
<<<<<<< HEAD
<<<<<<< HEAD
      emptyMessage: ''
=======
      emptyMessage: '',
      records: records // 确保records也在data中定义
>>>>>>> d89ffbc... 07-23统计图
=======
      emptyMessage: '',
      records: records // 确保records也在data中定义
>>>>>>> 0856d6b... 07-25完成小程序
    });
  },

  handleSubmit: function(e) {
    const formData = e.detail.value;
    if (!formData.amount || !formData.description || !formData.category || !formData.type) {
      wx.showToast({
        title: '请填写所有信息',
        icon: 'none'
      });
      return;
    }

    const newRecord = {
      ...formData,
      date: this.data.multiDateOptions[0][this.data.selectedDate[0]] + '-' +
            this.data.multiDateOptions[1][this.data.selectedDate[1]] + '-' +
            this.data.multiDateOptions[2][this.data.selectedDate[2]]
    };

    this.setData({
      latestRecord: newRecord,
      details: [...this.data.details, newRecord]
    });

    this.saveToLocal(newRecord);
  },

  bindDateChange: function(e) {
    this.setData({
      selectedDate: e.detail.value
<<<<<<< HEAD
<<<<<<< HEAD
    });
    this.calculateTotals();
  },
  calculateTotals: function() {
    // 直接从selectedDate中获取年和月
    const selectedYear = this.data.multiDateOptions[0][this.data.selectedDate[0]];
    const selectedMonth = this.data.multiDateOptions[1][this.data.selectedDate[1]].replace('月', ''); // 格式化月份为纯数字

    let totalExpense = 0;
    let totalIncome = 0;

    // 遍历records，累加指定年月的收支
    this.data.records.forEach(record => {
      // 解构记录的年和月
      const recordDate = record.date.split('-');
      const recordYear = recordDate[0];
      const recordMonth = recordDate[1];

      // 检查记录的年和月是否与选中的年和月匹配
      if (recordYear === selectedYear && recordMonth === selectedMonth) {
        if (record.type === '支出') {
          totalExpense += record.amount;
        } else if (record.type === '收入') {
          totalIncome += record.amount;
        }
      }
    });

    // 更新页面数据
    this.setData({
      totalExpense: totalExpense,
      totalIncome: totalIncome
    });
  },
=======
=======
>>>>>>> 0856d6b... 07-25完成小程序
    }, () => {
      this.calculateTotalsForSelectedDate();
    });
  },

  calculateTotalsForSelectedDate: function() {
    const [yearIndex, monthIndex] = this.data.selectedDate;
    const year = this.data.multiDateOptions[0][yearIndex];
    const month = this.data.multiDateOptions[1][monthIndex]; // 月份从0开始
  
    let totalExpense = 0;
    let totalIncome = 0;
  
    this.data.details.forEach(record => {
      const [recordYear, recordMonth] = record.date.split('-').map(Number);
      if (recordYear === year && recordMonth === month) {
        if (record.type === '支出') {
          totalExpense += parseFloat(record.amount);
        } else if (record.type === 'income') {
          totalIncome += parseFloat(record.amount);
        }
      }
    });
  
    this.setData({
      totalExpense: totalExpense.toFixed(1),
      totalIncome: totalIncome.toFixed(1)
    });
  },
  
<<<<<<< HEAD
>>>>>>> d89ffbc... 07-23统计图
=======
>>>>>>> 0856d6b... 07-25完成小程序
  navigateToAnotherPage: function() {
    wx.navigateTo({
      url: '/pages/anotherPage/anotherPage'
    });
  }
});